package com.godGuang.system.service.impl;

import com.godGuang.common.core.domain.Result;
import com.godGuang.common.system.domain.SysUser;
import com.godGuang.system.mapper.SendEmailMapper;
import com.godGuang.system.service.SendEmailService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;
import java.util.regex.Pattern;

/**
 * @Author The future is full of hope
 * @CreateTime 2024/1/16 0016 9:39
 * @PackageName:com.godGuang.system.service.impl
 * @ClassName: SendEmailServiceImpl
 */

@Service
public class SendEmailServiceImpl implements SendEmailService {

    @Autowired
    private SendEmailMapper sendEmailMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public Result sendResetPwdMail(String email) {
        if (!vehicleEmail(email)) {
            return Result.error("邮箱格式不正确");
        }
        SysUser sysUser = sendEmailMapper.selectUserByEmail(email);
        if (sysUser == null) {
            return Result.error("邮箱不存在,请注册！！！");
        }

        rabbitTemplate.convertAndSend("send_sms_queue_email", email, message -> {
            message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
            return message;
        });
        return Result.success(200,"验证码发送成功");
    }


    /**
     * 验证邮箱格式是否正确
     *
     * @param email 邮箱地址
     * @return 邮箱格式是否正确
     */
    private boolean vehicleEmail(String email) {
        Pattern compile = Pattern.compile("^(([^<>()\\\\.,;:\\s@\"]+(\\.[^<>()\\\\.,;:\\s@\"]+)*)|(\".+\"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$");
        return compile.matcher(email).matches();
    }
}
